#include "StationManagement.h"
//#include "station_mysql.h"
#include "app_mysql.h"
#include <iostream>

using namespace std;

void StationManagement::addStation(QString name)
{
    database_start("root", "");
    char *ch;
    ch = name.toLocal8Bit().data();
    addStation_mysql(ch);
    database_end();
}

void StationManagement::deleteStation(QString id)
{
    database_start("root", "");
    int i = id.toInt();
    deleteStation_mysql(i);
    database_end();

}

void StationManagement::modifyStation(QString id, QString name)
{
    database_start("root", "");
    int i = id.toInt();
    char *ch;
    ch = name.toLocal8Bit().data();
    modifyStation_mysql(i, ch);
    database_end();

}

Station StationManagement::queryStation(QString queryCondition)
{
    Station s;
//    cout<< "okha" <<endl;
    //Start with digit, call the id query
    if(queryCondition[0].isDigit()) {
        int i = queryCondition.toInt();

        database_start("root", "");
        s.ID = i;
        char *ch;
        ch = queryStation_mysql(i);
        s.name = QString(ch);
        database_end();
        cout << s.ID <<s.name.toLocal8Bit().data() << endl;
        return s;
    }

    //Start with letter, call the name query
    if(queryCondition[0].isLetter()) {
//        cout << "isl" <<endl;
        char *ch;
        ch = queryCondition.toLocal8Bit().data();

        database_start("root", "");
        s.ID = queryStation_mysql(ch);
        s.name = queryCondition;
        database_end();
        cout << s.ID <<s.name.toLocal8Bit().data() << endl;
        return s;
    }
}
